package com.coffeeloversupportlibrary.coffeelover.domain.article.cotegory;

import android.util.Log;

import com.coffeeloversupportlibrary.coffeelover.ApiService;
import com.coffeeloversupportlibrary.coffeelover.callback.ModelCallBack;
import com.coffeeloversupportlibrary.coffeelover.domain.article.entity.Article;
import com.coffeeloversupportlibrary.coffeelover.domain.article.entity.OriginData;
import com.coffeeloversupportlibrary.core.base.BaseModel;
import com.coffeeloversupportlibrary.core.data.IDataRepository;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author puyv
 * @createTime 2021/11/22 15:17
 * @projectName CoffeeLoverSupportLibrary
 * @className CategoryModel.java
 * @description TODO
 */
public class CategoryModel extends BaseModel {

    // TODO: 2021/11/23    bugs     articles 未经初始化使用
//    private List<Article> articles;

    public CategoryModel(IDataRepository dataRepository) {
        super(dataRepository);
    }
    public void getArticlesByType(ModelCallBack<List<Article>,String> callBack,int type){
        getRetrofitService(ApiService.class).requestArticleByType(type)
                .enqueue(new Callback<OriginData>() {
                    @Override
                    public void onResponse(Call<OriginData> call, Response<OriginData> response) {
                        if(response.body().isSuccessful()){
                            List<Article> list=new ArrayList<>();
                            for(OriginData.DataDTO.ArticleListDTO dto:response.body().getResult()){
                                Article article=new Article(dto.getArticleId(),dto.getArticleTitle(), dto.getArticleBanner(), dto.getArticleType(), dto.getCreateTime(), dto.getUpdateTime(), dto.getDeleted());
                                list.add(article);
                            }
                            callBack.onSuccess(add(list));
                        }else{
                            callBack.Failure(response.body().getMessage());
                        }
                    }

                    @Override
                    public void onFailure(Call<OriginData> call, Throwable t) {
                        // TODO: 2021/11/22 异常抛出信息
                        callBack.Failure(t.getMessage());
                    }
                });
    }
    // TODO: 2021/11/22    模拟更多数据
    private List<Article> add(List<Article> list){
       for(int i=0;i<5;i++){
          list.addAll(list);
       }
       return list;
    }
}
